package com.chushouya.controller.manager.admin;

import com.chushouya.common.constant.MenuSystemTypeEnum;
import com.chushouya.manager.dto.admin.menu.MenuListDTO;
import com.chushouya.manager.dto.admin.menu.MenuQuery;
import com.chushouya.manager.dto.admin.menu.MenuSaveDTO;
import com.chushouya.manager.dto.admin.menu.MenuStatusDTO;
import com.chushouya.manager.dto.admin.menu.MenuTreeDTO;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.manager.service.admin.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = ServiceType.PLATFORM + "平台菜单管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/menu")
@Slf4j
public class MenuController {

    private final Integer systemType = MenuSystemTypeEnum.ADMIN_SYSTEM.value();

    @Resource
    private MenuService menuService;

    @ApiOperation("获取菜单列表")
    @GetMapping("/list")
    public Response<List<MenuListDTO>> list(MenuQuery query) {
        query.setSystemType(systemType);
        return Response.success(menuService.selectMenuList(query));
    }

    @ApiOperation("根据菜单编号获取详细信息")
    @GetMapping("/getMenuDetail")
    public Response<MenuSaveDTO> getMenuDetail(Long menuId) {
        return Response.success(menuService.getMenuDetail(menuId));
    }

    @ApiOperation("获取菜单下拉树列表")
    @GetMapping("/getMenuTree")
    public Response<List<MenuTreeDTO>> getMenuTree() {
        return Response.success(menuService.getMenuTree(systemType));
    }

    @ApiOperation("加载对应角色菜单列表树")
    @GetMapping("/getRoleMenuTree")
    public Response<Map> getRoleMenuTree(Long roleId) {
        Map<String, Object> roleMenuTree = new HashMap<>();
        roleMenuTree.put("checkNode", menuService.selectMenuIdListRoleId(roleId));
        roleMenuTree.put("menuTree", menuService.getMenuTree(systemType));
        return Response.success(roleMenuTree);
    }

    @ApiOperation("新增菜单")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Response<Long> add(@Validated @RequestBody MenuSaveDTO menu) {
        menu.setSystemType(systemType);
        return Response.success(menuService.addMenu(menu));
    }

    @ApiOperation("修改菜单")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<Long> edit(@Validated @RequestBody MenuSaveDTO menu) {
        menu.setSystemType(systemType);
        return Response.success(menuService.editMenu(menu));
    }

    @ApiOperation("删除菜单")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Response<Void> remove(Long menuId) {
        menuService.deleteMenu(menuId);
        return Response.success();
    }

    @ApiOperation("切换菜单状态")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public Response<Void> changeStatus(@Validated @RequestBody MenuStatusDTO menuStatusDTO) {
        menuService.changeMenuStatus(menuStatusDTO);
        return Response.success();
    }
}
